package com.javaweb



import com.alibaba.fastjson.JSON
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.javaweb.mapper.SysMessageMapper
import com.javaweb.mapper.SysVoteMapper
import com.javaweb.mapper.VoteOptionMapper
import com.javaweb.pojo.SysMessage
import com.javaweb.pojo.VoteOption
import com.javaweb.pojo.VoteRecord
import com.javaweb.service.*
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest

@SpringBootTest
class JavaWebApplicationTests{
    @Autowired
    lateinit var sysUserService: SysUserService
    @Autowired
    lateinit var sysVoteMapper: SysVoteMapper
    @Autowired
    lateinit var voteOptionMapper: VoteOptionMapper
    @Autowired
    lateinit var messageService: SysMessageMapper
    @Autowired
    lateinit var sysVoteService: SysVoteService
    @Autowired
    lateinit var voteRecordService: VoteRecordService
    @Test
    fun contextLoads() {
        for (sysVote in sysVoteMapper.selectList(null)){
            val voteOption = VoteOption()
            val message = SysMessage()
            voteOption.voteId = sysVote.voteId
            message.voteId = sysVote.voteId
            sysVote.message = messageService.selectList(QueryWrapper<SysMessage>().setEntity(message))
            sysVote.options = voteOptionMapper.selectList( QueryWrapper<VoteOption>().setEntity(voteOption))
            (sysVote.message as MutableList<SysMessage>?)?.forEach {
                it.userName = sysUserService?.getById(it.userId)?.userName
            }
            println(JSON.toJSONString(sysVote, true))
        }
    }

    @Test
    fun test(){
        val records = voteRecordService.list(
            QueryWrapper<VoteRecord>().inSql(
                "option_id",
                "select option_id from sys_vote where vote_id = ".plus(1)
            )
        )
        records.forEach {
            println(JSON.toJSONString(it))
        }
    }

    @Test
    fun test2(){
        val eq = QueryWrapper<VoteRecord>()
           eq.eq("user_id", 3).and {
               it.inSql("option_id", "select option_id from vote_option where vote_id = ".plus(6))
           }

        val list = voteRecordService.list(
           eq

        )
        println(JSON.toJSONString(list,true))
    }
}
